package org.polaris.authority.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.polaris.authority.dto.LoginDTO;
import org.polaris.authority.service.ILoginService;
import org.polaris.basic.tool.BasicMap;
import org.polaris.org.domain.Employee;
import org.polaris.org.mapper.EmployeeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Primary
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Map<String, Object> selectByName(LoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        HashMap<String, Object> map = new HashMap<>();
        if (StringUtils.isEmpty(username) && username.equals("")){
            throw new RuntimeException("用户名为空");
        }
        if (StringUtils.isEmpty(password) && password.equals("")){
            throw new RuntimeException("密码为空");
        }
        Employee employee = employeeMapper.selectByName(username);

        if (Objects.isNull(employee)){
            throw new RuntimeException("查无此人");
        }

        if (!employee.getPassword().equals(password)){
            throw new RuntimeException("密码错误");
        }

        String token = UUID.randomUUID().toString();
        BasicMap.loginMap.put(token, employee);
        map.put("token",token);
        employee.setPassword("");
        map.put("user", employee);

        return map;
    }
}
